using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SaleManagement.ImportFile.Common;
using SaleManagement.Domains;

namespace SaleManagement.ImportFile.Forms
{
  public partial class MainForm : BaseForm
  {
    #region Properties
    LoginForm m_loginForm;
    #endregion

    #region Constructor
    public MainForm()
    {
      InitializeComponent();
    }
    #endregion

    #region Events
    private void MainForm_Load(object sender, EventArgs e)
    {
      this.Hide();
      m_loginForm = new LoginForm();
      m_loginForm.FormClosing += new FormClosingEventHandler(loginForm_FormClosing);
      m_loginForm.ShowDialog();
    }

    public void loginForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      if (m_loginForm.IsLoginOK)
      {
        LiabilitiesToolStripMenuItem_Click(null, null);
        this.Show();
      }
      else
      {
        this.Close();
        Application.Exit();
      }
    }

    private void LiabilitiesToolStripMenuItem_Click(object sender, EventArgs e)
    {
      LiabilitiesForm liabitiesForm = new LiabilitiesForm();
      liabitiesForm.LoginUser = m_loginForm.CurrentUser;
      AddForm(liabitiesForm);
    }
    #endregion

    #region Helpers
    private void AddForm(Form frmIsLoaded)
    {
      bool isLoaded = false;
      foreach (Form child in MdiChildren)
      {
        if (child.Text == frmIsLoaded.Text)
        {
          isLoaded = true;
          frmIsLoaded = child;
          break;
        }
      }

      if (!isLoaded)
      {
        frmIsLoaded.MdiParent = this;
        frmIsLoaded.WindowState = FormWindowState.Maximized;
        frmIsLoaded.Show();
      }
      else
      {
        frmIsLoaded.Focus();
      }
    }

    private void ClosedChildForm()
    {
      foreach (Form child in MdiChildren)
      {
        child.Close();
      }
    }
    #endregion
  }
}